package fr.umlv.m2.jee.spij.web.utils;

import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import java.util.List;

@Component("multiSelectUnitValidator")
public class MultiSelectUnitValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component,
                         Object value) throws ValidatorException {

        int maxSelections = Integer.parseInt((String) component.getAttributes()
                .get("maxSelections"));
        if (((List<String>) value).size() > maxSelections) {
            throw new ValidatorException(new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Limite dépassée",
                    "Trop d'options selectionnées"));
        }
    }

}
